import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;


public class HuffmanEncode 
{
	static HuffNode[] data=new HuffNode['z'-'a'+2];
	static char c;
	static String encodedText="";
	
	private static void processString(String inputString){
		if (inputString.contains(":")){
			inputString=inputString.toLowerCase();
			inputString=inputString.replace(" ", "");
			String[] line=inputString.split(":");
			char letter=line[0].charAt(0);
			data[letter-'a'+1]=new HuffNode(letter, line[1]);
		}
	}
	
	private static void encodeString(String word){
		String letterRep;
		word=word.toLowerCase();
		for (int i=0;i<word.length();i++){
			letterRep=data[word.charAt(i)-'a'+1].path;
			System.out.println(letterRep);
			encodedText=encodedText+letterRep;
		}
	}
	public static void main(String[] args)
	{
		String inputFile1 = args[0];
		//String inputFile1 = ".\\src\\Example2\\input2-2.txt";
		
		String inputFile2 = args[1];
		//String inputFile2 = ".\\src\\Example2\\out2-2.txt";
		
		String outputFile = args[2];
		//String outputFile = inputFile2+"_processed.txt";
		
		
		read_From_File(inputFile2,false);
		read_From_File(inputFile1,true);
		
		System.out.println("Full encoded text is:\n\n");
		
		
		write_To_File(outputFile, encodedText);
		
		
	}
	public static void read_From_File(String inputFilename,boolean isEncoding){
       	System.out.println("Reading from file " + inputFilename);
       
       	try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                    while(st.hasMoreTokens())
                    {
                    	String token = st.nextToken().trim();
                    	System.out.println(token);
                    	if (!isEncoding)
                    		processString(token);
                    	else
                    		encodeString(token);
             
                    }                   
                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }
	}
	private static void write_To_File(String outputFilename,String encodedText) {

        System.out.println("Writing to file " + outputFilename);
        
        
        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);
            System.out.println("writing....\n");
            char c;
            for (int i=0;i<encodedText.length();i++){
            	c=encodedText.charAt(i);
    			System.out.print(c);
    			ofw.write(c);
    			if ((i+1)%80==0){
    				System.out.println();
    				ofw.write("\r\n");
    			}
    		}

            ofw.close();
            
            System.out.println("\nWrote to file " + outputFilename);
            
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
	
}
